package translation;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import libraries.LibraryContainer;



public class Circuit {

	
	NetSystem netSys;
	PartSystem partSys;
	
	
	public NetSystem getNetSys() {
		return netSys;
	}
	public PartSystem getPartSys() {
		return partSys;
	}
	
	public void readCircuit(File partList, File NetList) throws IOException {
		//Part lists must always be read first since net list creation needs an existent part list
		partSys.readPartList(partList);
		netSys.readNetList(NetList);
	}
			
	public void writeCircuit(File output, String name) throws IOException {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new FileWriter(output));
			out.write("within ;\npackage Elect\n\n //  import Modelica.SIUnits.*;\n");
			out.write("model " + name + "\n");
			String comments = getPartSys().writePartList(out);
			out.write("equation\n");
			getNetSys().writeNetList(out);
			out.write(comments);
			out.write("end " + name + ";\n");
			out.write("end Elect;\n");
		} finally {
			if (out != null) {
				out.close();
			}
		}
		
				
		//String out = "within ;\npackage Elect\n\n //  import Modelica.SIUnits.*;\n";
		//out += "model " + name + "\n";
		//out += getPartSys().writePartList(out)
		
	}
	
	
	public Circuit(LibraryContainer lib) {
		super();
		this.partSys = new PartSystem(lib);
		this.netSys = new NetSystem(this);
	}
	
	public static void testRun(){
//hardcode
		boolean HC = false;
		if (HC) {
//			LibraryContainer lib = LibraryContainer.testLibraryContainer();
			LibraryContainer lib = new LibraryContainer();
			try {
				lib.readLibrary(new File ("D:\\Bachelor\\EAGLE\\Beispiele\\Test\\stuffLib"));
				
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			Circuit circ = new Circuit(lib);
			try{
				lib.writeLibrary(new File ("D:\\Bachelor\\EAGLE\\Beispiele\\Test\\stuffLib2"), "Test_Library");
				circ.getPartSys().readPartList(new File ("D:\\Bachelor\\EAGLE\\Beispiele\\Test\\stuffPart"));
				circ.getNetSys().readNetList(new File ("D:\\Bachelor\\EAGLE\\Beispiele\\Test\\stuffNet"));
				circ.writeCircuit(new File ("D:\\Bachelor\\EAGLE\\Beispiele\\Test\\stuffDYM.mo"), "Stuff");
				
			}  catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}

		} else {
			//		LibraryContainer lib = LibraryContainer.testLibraryContainer();
			LibraryContainer lib = new LibraryContainer();
			try {
				//			lib.readLibrary(new File ("D:\\Bachelor\\EAGLE\\Beispiele\\Test\\stuffLib"));

				lib.readLibrary(new File ("Test_InputLibrary.edl"));
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			Circuit circ = new Circuit(lib);
			try{
				//			lib.writeLibrary(new File ("D:\\Bachelor\\EAGLE\\Beispiele\\Test\\stuffLib2"), "Test_Library");
				//			circ.getPartSys().readPartList(new File ("D:\\Bachelor\\EAGLE\\Beispiele\\Test\\stuffPart"));
				//			circ.getNetSys().readNetList(new File ("D:\\Bachelor\\EAGLE\\Beispiele\\Test\\stuffNet"));
				//			circ.writeCircuit(new File ("D:\\Bachelor\\EAGLE\\Beispiele\\Test\\stuffDYM.mo"), "Stuff");

				lib.writeLibrary(new File ("Test_OutputLibrary.edl"), "Test_Library");
				circ.getPartSys().readPartList(new File ("Test_Partlist"));
				circ.getNetSys().readNetList(new File ("Test_Netlist"));
				circ.writeCircuit(new File ("Test_DY.mo"), "Test_Circuit");
			}  catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
		}
	}
	
	public static void GUITestRun(File partList, File netList, File save){
		//hardcode
				boolean HC = true;
				if (HC) {
//					LibraryContainer lib = LibraryContainer.testLibraryContainer();
					LibraryContainer lib = new LibraryContainer();
					try {
						lib.readLibrary(new File ("D:\\Bachelor\\EAGLE\\Beispiele\\Test\\stuffLib"));
						
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					Circuit circ = new Circuit(lib);
					try{
						lib.writeLibrary(new File ("D:\\Bachelor\\EAGLE\\Beispiele\\Test\\stuffLib2"), "Test_Library");
						circ.getPartSys().readPartList(partList);
						circ.getNetSys().readNetList(netList);
						circ.writeCircuit(save, "Test file");
						
					}  catch (IOException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}

				}
			}
	
	public static void main(String[] args) {
		testRun();
	}
	
}
